package at.ac.tuwien.ifs.calendit.basis.gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;


import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import at.ac.tuwien.ifs.calendit.basis.helper.Constants;


public class Login extends JFrame implements ActionListener{
	
	private static final long serialVersionUID = -4569798644433941724L;
	private JTextField textfAcc;
	private JButton loginButton;
	private JPanel panel;
	private JPasswordField pwField;
	private JLabel lbl_pw;
	private JLabel lbl_AccNr;
	private JLabel lbl_loginFail;
	private JLabel img_logo;
	
	
	private static Log logger = LogFactory.getLog(Login.class);
//	private static Constants consta= new Constants();
	private boolean correctLogin = false;
	
	public Login()
	{	
		//Look and feel
        try {UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());}
        catch (ClassNotFoundException e) {}
        catch (InstantiationException e) {}
        catch (IllegalAccessException e) {}
        catch (UnsupportedLookAndFeelException e) {}
        
		initGUI();
	}
	
    private void initGUI() {
    		this.setTitle("Calendit - Login");
	    	this.setPreferredSize(new java.awt.Dimension(500, 400));
	    	//this.setBorder(BorderFactory.createLineBorder( Color.orange));
	    	BorderLayout thisLayout = new BorderLayout();
	    	this.setLayout(thisLayout);
	    	setComponents();
	    	this.add(logo(), BorderLayout.NORTH);
	    	this.add(addComponents(), BorderLayout.CENTER);
	    	
	    	this.setBackground(Color.white);
	    	
    }
    
    private void setComponents(){
    	panel = new JPanel();
    	panel.setPreferredSize(new Dimension(450,350));
		//panel.setBounds(0, 0, 450, 350);
		panel.setBackground(Color.white);
		panel.setLayout(null);
		
    	lbl_AccNr = new JLabel("AccountNr", JLabel.RIGHT);
    	panel.add(lbl_AccNr);
    	//lbl_AccNr.setPreferredSize(new Dimension(100, 22));
		lbl_AccNr.setBounds(135, 60, 100, 22);
		lbl_AccNr.setFocusable(true);
		
		lbl_pw = new JLabel("Passwort", JLabel.RIGHT);
		panel.add(lbl_pw);
		lbl_pw.setBounds(135, 100, 100, 22);
		lbl_pw.setFocusable(true);
		
		textfAcc = new JTextField();
		panel.add(textfAcc);
		textfAcc.setBounds(245, 60, 100, 22);
    	
		pwField = new JPasswordField();
		panel.add(pwField);
		pwField.setBounds(245, 100, 100, 22);
		
		loginButton = new JButton("Login");
		panel.add(loginButton);
		loginButton.addActionListener(this);
		loginButton.setBounds(255, 150, 80, 25);
		
		lbl_loginFail = new JLabel("Login fehlgeschlagen - AccountNr oder Passwort inkorrekt!");
		lbl_loginFail.setForeground(Color.red);
		panel.add(lbl_loginFail);
    	lbl_loginFail.setBounds(115, 15, 350, 25);
    	lbl_loginFail.setVisible(false);

    }
    
    private JPanel addComponents(){
    	
		//panel.setPreferredSize(new java.awt.Dimension(1027, 609));
    	setComponents();
    	return panel;
    }

    
    private JPanel logo(){
    	JPanel logo_panel = new JPanel();
    	img_logo = new JLabel();
		logo_panel.add(img_logo);
		img_logo.setIcon(Constants.ICON_LOGO);
		img_logo.setBounds(304, 12, 219, 115);
		logo_panel.setBackground(Color.WHITE);
		
		//img_logo.setPreferredSize(new java.awt.Dimension(228, 88));
		return logo_panel;
    	
    }
    
	public void actionPerformed(ActionEvent e) {
		LoginTableModel login = new LoginTableModel();
		int id = 0;
		int rechte = 0;
		try {
			id = Integer.parseInt(textfAcc.getText());
			Constants.setUserId(id);
		} catch (Exception e1) {
			logger.error("failed in parsing ID to integer!");
		}
		String pw = String.valueOf(pwField.getPassword());
		correctLogin = login.checkLogin(id, pw);
		
		if (correctLogin){
			logger.info("Login combination is correct!");
			this.dispose();
			//new Calendit("normal");
			rechte = login.checkRechte(id);
			if (rechte == 3){
				logger.info("rechte: admin");
				new Calendit("admin", id);
			} else {
				logger.info("rechte: normaler MA");
				new Calendit("normal", id);
			}
			
		} else {
			logger.debug("Login failed! id \""+ id +"\" and pw \""+pw+"\" does not match!");
			lbl_loginFail.setVisible(true);
		}
	}

}
